// mine/order/list/list.js
import Toast from '../../../ui/toast/index'
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pic_url:app.url.base_url,
        list:[],
        page:{
            page:1,
            type:0,//0-全部，1-待付款，2-待发货,3-待收货,4-已完成
        },
        loading:false,
        send_visible:false,
        kuaidi:{},//快递信息
    },

    //跳转评价
    to_pj(e){
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/mine/rate/index?id='+id,
        })
    },

    //配送
    to_peisong(e){
        let id = e.currentTarget.dataset.id;
        this.setData({
            kuaidi:{},
        });
        app.login.request("index.php?r=user/order/send",{id:id}).then((res)=>{
            if(res.data.result){
                this.setData({
                    kuaidi:res.data.data,
                    send_visible:true,
                });
            }else{
                Toast({
                    context:this,
                    selector:"#toast",
                    message:res.data.data,
                });
            }
        });
    },

    onVisibleChange(){
        this.setData({
            send_visible:false,
        });
    },

    //确认收货
    change_get(e){
        let id = e.currentTarget.dataset.id;
        wx.showModal({
          title: '提示',
          content: '确定收到货了么',
          complete: (res) => {
            if (res.confirm) {
              app.login.request("index.php?r=user/order/get",{id:id}).then((res)=>{
                if(res.data.result){
                    Toast({
                        context:this,
                        selector:"#toast",
                        message:res.data.data,
                    });
                    this.get_list();
                }else{
                    Toast({
                        context:this,
                        selector:"#toast",
                        message:res.data.data,
                    });
                }
              });
            }
          }
        })
    },

    onTabsClick(e){
        this.data.page.type = e.detail.value;
        this.setData({
            list:[],
        });
        this.data.page.page=1;
        this.get_list();
    },

    //获取列表
    get_list(){
        this.setData({
            loading:true,
        });
        app.login.request("index.php?r=user/order/list",{page:this.data.page}).then((res)=>{
            this.setData({
                loading:false,
            });
            console.log(res);
            if(res.data.result==1){
                let list = res.data.data;
                let order_status="已完成";
                for(let i=0;i<list.length;i++){
                    if(!list[i].is_pay){
                        order_status = "待付款";
                        list[i].order_status = order_status;
                        continue;
                    }
                    if(!list[i].is_send){
                        order_status = "待发货";
                        list[i].order_status = order_status;
                        continue;
                    }
                    if(!list[i].is_get){
                        order_status = "待收货";
                        list[i].order_status = order_status;
                        continue;
                    }
                    if(!list[i].is_end){
                        order_status = "待完成";
                        list[i].order_status = order_status;
                        continue;
                    }
                    list[i].order_status = order_status;
                }
                this.data.list = this.data.list.concat(list);
                this.setData({
                    list:this.data.list,
                });
                this.data.page.page++;
            }
            if(res.data.result==2){
                Toast({
                    context:this,
                    selector:"#toast",
                    message:"没有更多内容"
                });
            }
        });
    },

    //下拉刷新
    onRefresh(){
        setTimeout(() => {
            this.get_list();
          }, 1000);
    },

    //触底刷新
    onScrollbtm(){
        console.log(this.data.page.page);
        this.get_list();
    },

    //跳转详情，支付
    to_order(e){
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/order/index/index?id='+id,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
        app.get_name();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.data.page.page=1;
        this.setData({
            list:[],
        });
        this.get_list();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})